﻿/*
-- 仿 ms 效果
-- 下载软件 on 2009-12-16 于深圳
*/
function $MsShow(sourceId, targetId) {
    var tools = {
        contains: function(a, b) {
            try {
                return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16);
            } catch (e) { return false; }
        },
        ev: function() {
            if (document.all) { return window.event; }
            var func = this.ev.caller;
            while (func != null) {
                var arg0 = func.arguments[0];
                if (arg0 instanceof Event) { return arg0; }
                func = func.caller;
            }
            return null;
        },
        loc: function(o) {
            if (arguments.length != 1 || o == null) return null;
            var t = o.offsetTop, l = o.offsetLeft;
            var w = o.offsetWidth, h = o.offsetHeight;
            while (o = o.offsetParent) { t += o.offsetTop; l += o.offsetLeft; }
            return { AbsoluteTop: t, AbsoluteLeft: l, OffsetWidth: w, OffsetHeight: h };
        },
        quadeasein: function(t, b, c, d) { return c * (t /= d) * t + b; },
        whtl: function(o, w, h, t, l) {
            if (o) {
                o.style.width = w + "px"; o.style.height = h + "px";
                o.style.top = t + "px"; o.style.left = l + "px";
            }
        },
        show: function(o) { o.style.visibility = "visible"; },
        hide: function(o) { o.style.visibility = "hidden"; }
    };

    var div, timer, timer1, timer2, s_loc, t_loc, d_loc, isShow = false;
    var s = document.getElementById(sourceId), t = document.getElementById(targetId);
    s_loc = tools.loc(s); t_loc = tools.loc(t);
    div = document.createElement("div");
    div.style.cssText = "position: absolute; z-index: 4567; border: solid 1px #666; font-size: 1px;";
    tools.whtl(div, 1, 1, s_loc.AbsoluteTop + s_loc.OffsetHeight / 2 - 1, s_loc.AbsoluteLeft + s_loc.OffsetWidth / 2 - 1);
    document.body.appendChild(div);
    d_loc = tools.loc(div); tools.hide(div); tools.hide(t);
    s.onmouseover = t.onmouseover = function() { clearTimeout(timer); if (!isShow) { show(); } }
    s.onmouseout = t.onmouseout = function() {
        if (isShow) { hide(); } else { setTimeout(function() { isShow = false; hide(); }, 150); }
    }
    function show() {
        clearInterval(timer2);
        var W, H, T, L, Ti = 0, D = 3;
        tools.show(div); isShow = true;
        timer1 = setInterval(function() {
            W = tools.quadeasein(Ti, d_loc.OffsetWidth, t_loc.OffsetWidth - d_loc.OffsetWidth, D);
            H = tools.quadeasein(Ti, d_loc.OffsetHeight, t_loc.OffsetHeight - d_loc.OffsetHeight, D);
            T = tools.quadeasein(Ti, d_loc.AbsoluteTop, t_loc.AbsoluteTop - d_loc.AbsoluteTop, D);
            L = tools.quadeasein(Ti, d_loc.AbsoluteLeft, t_loc.AbsoluteLeft - d_loc.AbsoluteLeft, D);
            tools.whtl(div, W, H, T, L);
            if (Ti++ >= D) { tools.hide(div); tools.show(t); clearInterval(timer1); }
        }, 50);
    }
    function hide() {
        try {
            var evt = tools.ev(), to = evt.toElement || evt.relatedTarget;
            if (to === t || to === s || tools.contains(s, to) || to === div) { return; }
        } catch (e) { }
        timer = setTimeout(function() {
            clearInterval(timer1);
            var W, H, T, L, Ti = 0, D = 3;
            tools.hide(t); tools.show(div); isShow = false;
            timer2 = setInterval(function() {
                W = tools.quadeasein(Ti, t_loc.OffsetWidth, d_loc.OffsetWidth - t_loc.OffsetWidth, D);
                H = tools.quadeasein(Ti, t_loc.OffsetHeight, d_loc.OffsetHeight - t_loc.OffsetHeight, D);
                T = tools.quadeasein(Ti, t_loc.AbsoluteTop, d_loc.AbsoluteTop - t_loc.AbsoluteTop, D);
                L = tools.quadeasein(Ti, t_loc.AbsoluteLeft, d_loc.AbsoluteLeft - t_loc.AbsoluteLeft, D);
                tools.whtl(div, W, H, T, L);
                if (Ti++ >= D) { tools.hide(div); clearInterval(timer2); }
            }, 50);
        }, 300);
    }
}